home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 526-550 / disk_527 / toolmanager / source / starttools.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  228 lines

  1. /*
  2.  * starttools.c   V1.4
  3.  *
  4.  * CLI & WB startup procedure
  5.  *
  6.  * (c) 1991 by Stefan Becker
  7.  *
  8.  */
  9. #include "ToolManager.h"
  10.  
  11. /* Start tool as a CLI process */
  12. static BOOL StartCLITool(struct AppMessage *msg, struct ToolNode *tn,
  13.                          char *name)
  14. {
  15.  BPTR fl;                       /* AmigaDOS file handle */
  16.  BOOL rc=TRUE;
  17.  char cmd[NAMELEN];             /* Buffer for command line */
  18.  int cmdlen;                    /* Command line length */
  19.  char dir[NAMELEN];
  20.  struct WBArg *wa=msg->am_ArgList;
  21.  int i;
  22.  
  23.  fl=CurrentDir(tn->tn_DirLock); /* Change to tool's directory */
  24.  
  25.  strcpy(cmd,name);              /* Command name first */
  26.  cmdlen=strlen(cmd);
  27.  
  28.  for (i=msg->am_NumArgs; i; i--,wa++)
  29.   {
  30.    char *cp;
  31.    int namelen;
  32.  
  33.    if (!wa->wa_Lock) continue;  /* Skip arguments which don't support locks! */
  34.  
  35.    if (cmdlen>NAMELEN-2) break; /* Append a space for each parameter */
  36.    strcat(cmd," ");
  37.    cmdlen++;
  38.  
  39.    /* Build parameter from Lock & name */
  40.    if (SameLock(tn->tn_DirLock,wa->wa_Lock)==LOCK_SAME) cp=wa->wa_Name;
  41.    else
  42.     {
  43.      if (!NameFromLock(wa->wa_Lock,dir,NAMELEN)) continue;
  44.      if (!AddPart(dir,wa->wa_Name,NAMELEN)) continue;
  45.      cp=dir;
  46.     }
  47.  
  48.    namelen=strlen(cp);
  49.  
  50.    if (strchr(cp,' '))          /* Special case: Space in a filename */
  51.     if (namelen>NAMELEN-3) break;
  52.     else
  53.      {
  54.       strins(cp,"\"");          /* Set parameter in double quotes */
  55.       strcat(cp,"\"");
  56.       namelen+=2;
  57.      }
  58.  
  59.    if (cmdlen+namelen>NAMELEN-2) break;
  60.    strcat(cmd,cp);              /* Append parameter */
  61.    cmdlen+=namelen;             /* New command line length */
  62.   }
  63.  
  64.  /* Start tool */
  65.  if (SystemTags(cmd,SYS_Input,Open("NIL:",MODE_NEWFILE),
  66.                     SYS_Output,Open("NIL:",MODE_NEWFILE),
  67.                     SYS_Asynch,TRUE, /* Run tools asynchronously */
  68.                     NP_StackSize,tn->tn_Stack,
  69.                     TAG_DONE)==-1)
  70.   rc=FALSE; /* An error occured */
  71.  
  72.  CurrentDir(fl);                /* Change to old directory */
  73.  return(rc);
  74. }
  75.  
  76. /* Start tool as a WB process */
  77. static BOOL StartWBTool(struct AppMessage *msg, struct ToolNode *tn,
  78.                         char *name)
  79. {
  80.  BPTR fl;                        /* AmigaDOS file handle */
  81.  register struct WBStartup *wbs; /* WBStartup message for tool */
  82.  struct DiskObject *tdob;        /* Tool icon */
  83.  LONG ssize;                     /* StackSize, default */
  84.  struct MsgPort *proc;           /* Process descriptor for tool */
  85. /* struct Process *proc;  Process descriptor for CreateNewProc */
  86.  struct WBArg *wbad,*wbas;       /* Pointers to WB arguments */
  87.  char *proname=NULL;             /* Name of Project icon */
  88.  int i;
  89.  
  90.  /* Allocate memory for WBStartup */
  91.  if (!(wbs=calloc(sizeof(struct WBStartup)+
  92.                   sizeof(struct WBArg)*(msg->am_NumArgs+2),1))) return (FALSE);
  93.  
  94.  /* Change to tool's directory */
  95.  fl=CurrentDir(tn->tn_DirLock);
  96.  
  97.  /* Is it a project? */
  98.  if (tdob=GetDiskObject(name))
  99.   if (tdob->do_Type==WBPROJECT)
  100.    {
  101.     proname=name;                      /* Save original name */
  102.     name=strdup(tdob->do_DefaultTool); /* Get name of default tool */
  103.     FreeDiskObject(tdob);
  104.     if (!name) goto e1;                /* Enough memory? */
  105.     tdob=GetDiskObject(name);          /* Get icon of the default tool */
  106.    }
  107.  
  108.  /* Is it a tool? */
  109.  ssize=tn->tn_Stack;
  110.  if (tdob)
  111.   {
  112.    if (tdob->do_Type==WBTOOL)          /* Only tools supply this information */
  113.     {
  114.      if (tdob->do_ToolWindow) wbs->sm_ToolWindow=strdup(tdob->do_ToolWindow);
  115.      if (tdob->do_StackSize>ssize) ssize=tdob->do_StackSize;
  116.     }
  117.  
  118.    FreeDiskObject(tdob);
  119.   }
  120.  
  121.  /* Load tool code */
  122.  if (!(wbs->sm_Segment=NewLoadSeg(name,NULL))) goto e2;
  123.  
  124.  /* Build WBStartup message */
  125.  /* wbs->sm_Message.mn_Node.ln_Type=NT_MESSAGE; PutMsg() does this for us! */
  126.  wbs->sm_Message.mn_ReplyPort=MyMP;
  127.  wbs->sm_Message.mn_Length=sizeof(struct WBStartup);
  128.  wbs->sm_NumArgs=msg->am_NumArgs+1;
  129.  wbs->sm_ArgList=wbs+1;             /* WBArg array starts after WBStartup */
  130.  
  131.  /* Initialize WBArg pointers */
  132.  wbas=msg->am_ArgList;
  133.  wbad=wbs->sm_ArgList;
  134.  
  135.  /* 1. argument is the tool itself! */
  136.  if (!(wbad->wa_Lock=DupLock(tn->tn_DirLock))) goto e3;
  137.  if (!(wbad->wa_Name=strdup(name))) goto e4;
  138.  wbad++;
  139.  
  140.  /* If tool is a project, add it as 2. parameter to the WBArg list */
  141.  if (proname)
  142.   {
  143.    if (!(wbad->wa_Lock=DupLock(tn->tn_DirLock))) goto e4;
  144.    if (!(wbad->wa_Name=strdup(proname))) goto e4;
  145.    wbad++;
  146.    wbs->sm_NumArgs++;
  147.   }
  148.  
  149.  /* Copy WB arguments */
  150.  for (i=msg->am_NumArgs; i; i--,wbas++,wbad++)
  151.   {
  152.    if (!(wbad->wa_Lock=DupLock(wbas->wa_Lock)))
  153.     {
  154.      wbad--;             /* Skip parameters, which don't support a lock */
  155.      wbs->sm_NumArgs--;
  156.      continue;           /* Next parameter */
  157.     }
  158.  
  159.    /* Sanity check for name string... Enforcer is watching you! */
  160.    if (!wbas->wa_Name || !(wbad->wa_Name=strdup(wbas->wa_Name))) goto e4;
  161.   }
  162.  
  163.  /* Create process */
  164.  if (!(wbs->sm_Process=CreateProc(wbs->sm_ArgList->wa_Name,0,wbs->sm_Segment,
  165.                                   ssize))) goto e4;
  166.  
  167. /* if (!(proc=CreateNewProcTags(NP_Seglist,wbs->sm_Segment,
  168.                               NP_FreeSeglist,TRUE,
  169.   Maybe someday I will know   NP_StackSize,ssize,
  170.   what Tags I need to make    NP_Name,wbs->sm_ArgList->wa_Name,
  171.   CreateNewProc() behave      NP_CloseInput,FALSE,
  172.   like CreateProc()           NP_CloseOutput,FALSE,
  173.                               TAG_DONE))) goto e4; */
  174.  
  175.  /* Send WBStartup message to tool */
  176. /* wbs->sm_Process=&proc->pr_MsgPort; for CreateNewProc() */
  177.  PutMsg(wbs->sm_Process,(struct Message *) wbs);
  178.  if (proname) free(name);       /* If project, then free default tool name */
  179.  CurrentDir(fl);                /* Change to old directory */
  180.  wbactive++;                    /* Tool started! */
  181.  return(TRUE);
  182.  
  183.  /* An error occurred. Free all resources */
  184. e4: wbas=wbs->sm_ArgList;
  185.     for (i=wbs->sm_NumArgs; i; i--,wbas++)
  186.      {
  187.       UnLock(wbas->wa_Lock);
  188.       if (wbas->wa_Name) free(wbas->wa_Name);
  189.      }
  190. e3: UnLoadSeg(wbs->sm_Segment);
  191. e2: if (proname) free(name);
  192. e1: CurrentDir(fl);
  193.     free(wbs);
  194.     return(FALSE);
  195. }
  196.  
  197. /* Start a tool with arguments */
  198. void StartTool(struct ToolNode *tn, struct AppMessage *msg)
  199. {
  200.  BOOL rc=TRUE;
  201.  char *cp;
  202.  struct AppMessage emsg;
  203.  
  204.  /* No Parameters? */
  205.  if (!msg || tn->tn_Flags&TNFLAGS_NARG)
  206.   {
  207.    emsg.am_NumArgs=0;       /* Dummy AppMessage has 0 arguments */
  208.    msg=&emsg;               /* Replace pointer to real message */
  209.   }
  210.  
  211.  /* Get tool name */
  212.  if (!(cp=tn->tn_RealName))
  213.   cp=tn->tn_Node.ln_Name;
  214.  
  215.  /* Tool type? */
  216.  switch(tn->tn_Type)
  217.   {
  218.    case TNTYPE_CLI:
  219.     rc=StartCLITool(msg,tn,cp);
  220.     break;
  221.    case TNTYPE_WB:
  222.     rc=StartWBTool(msg,tn,cp);
  223.     break;
  224.   }
  225.  
  226.  if (!rc) DisplayBeep(NULL); /* An error occured */
  227. }
  228.